package com.key.win.system.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.key.win.system.model.SysDevice;
import com.key.win.system.model.SysOrgan;
import com.key.win.system.model.SysOrganDevice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;


/**
 * 用户机构关系
 */
@Mapper
public interface SysOrganDeviceDao extends BaseMapper<SysOrganDevice> {

    int deleteOrganDevice(@Param("organId") String organId, @Param("deviceId") String deviceId);

    /**
     * 根据用户id获取Device
     *
     * @param organId
     * @return
     */
    @Select("select o.* from sys_organ_device uo inner join sys_device o on uo.device_id = o.id where uo.organ_id = #{organId} and uo.enable_flag = 1 and o.enable_flag = 1")
    List<SysDevice> findDeviceByOrganId(String organId);


    /**
     * 根据用户id获取Device
     *
     * @param deviceId
     * @return
     */
    @Select("select u.* from sys_organ_device uo inner join sys_organ u on uo.organ_id = u.id where uo.device_id = #{deviceId} and uo.enable_flag = 1 and u.enable_flag = 1")
    List<SysOrgan> findOrganByDeviceId(String deviceId);

    /**
     * 根据用户ids 获取
     *
     * @param organIds
     * @return
     */

    @Select("<script>select r.*,uo.organ_id from sys_organ_device uo inner join sys_device r on r.id = uo.device_id where  uo.enable_Flag = 1 and r.enable_Flag = 1 and uo.organ_id IN " +
            " <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> " +
            " #{item} " +
            " </foreach>" +
            "</script>")
    List<SysDevice> findDeviceByOrganIds(List<String> organIds);

    /**
     * 根据用户ids 获取
     *
     * @param deviceIds
     * @return
     */

    @Select("<script>select u.* from sys_organ_device uo inner join sys_organ u on u.id = uo.organ_id where u.enable_Flag = 1 and uo.enable_Flag = 1  and uo.device_id IN " +
            " <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> " +
            " #{item} " +
            " </foreach>" +
            "</script>")
    List<SysOrgan> findOrganByDeviceIds(List<String> deviceIds);

    void saveBatchOrganIdAndDeviceIds(@Param("organId") String organId, @Param("deviceIds") Set<String> deviceIds);

    void saveBatchOrganIdsAndDeviceId(@Param("organIds") Set<String> organIds, @Param("deviceId") String deviceId);
}
